<%@ page language="java" import="java.util.*" pageEncoding="Utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>客户管理</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
 <link rel="stylesheet" href="${ctx}/resources/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<link rel="stylesheet" type="text/css" href="${ctx}/resources/easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/resources/css/wu.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/resources/css/icon.css" />
<script type="text/javascript" src="${ctx}/resources/js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="${ctx}/resources/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${ctx}/resources/easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="${ctx}/resources/zTree/js/jquery.ztree.core.js"></script>
</head>

<body>
	 
	<!-- 查询条件开始 -->
	<div class="easyui-panel" data-options="title:'查询条件',iconCls:'icon-search'" style="width:100%;">
		<form id="searchFrm" method="get">
			<table width="100%" cellpadding="10" >
				<tr>
					<td width="33%">
					<input class="easyui-textbox" name="identity"
							data-options="label:'身份证号:',labelPosition:'left',labelAlign:'right'"
							style="width:80%;">
					</td>
					<td width="33%"> 
					<input class="easyui-textbox" name="custname"
							data-options="label:'客户姓名:',labelPosition:'left',labelAlign:'right'"
							style="width:80%;">
					</td>
					<td width="33%">
					<input class="easyui-textbox" name="address"
							data-options="label:'客户地址:',labelPosition:'left',labelAlign:'right'"
							style="width:80%;">
					</td>
				</tr>
				<tr>
					<td width="33%">
					<input class="easyui-textbox" name="phone"
							data-options="label:'客户电话:',labelPosition:'left',labelAlign:'right'"
							style="width:80%;">
					</td>
					<td width="33%">
					<input class="easyui-textbox" name="career"
							data-options="label:'客户职位:',labelPosition:'left',labelAlign:'right'"
							style="width:80%;">
					</td>
					<td width="33%">
						<label class="textbox-label" style="text-align: right;">
        				客户性别:
        			</label>
           			 <input class="easyui-radiobutton" name="sex" value="0">&nbsp;&nbsp;女&nbsp;&nbsp;&nbsp;&nbsp;
           			 <input class="easyui-radiobutton" name="sex" value="1">&nbsp;&nbsp;男
					</td>
				</tr>
				<tr>
					<td colspan="3" align="center">
						<a href="javascript:void(0)" class="easyui-linkbutton" onclick="doSearch()" data-options="iconCls:'icon-search'">查询</a>
						<a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:$('#searchFrm').form('clear')" data-options="iconCls:'icon-reload'">重置</a>
					</td>
				</tr>
			</table>
		</form>
	</div>
	<!-- 查询条件结束 -->
	 <div style="height: 2px"></div>
	 <!-- 数据表格开始 -->
    <table id="table" class="easyui-datagrid" style="width:100%;height:300px">
    </table>
	<div id="tb">
	<a href="javascript:void(0)" class="easyui-linkbutton" id="addCustomer" data-options="iconCls:'icon-add',plain:true">添加客户</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" id="updateCustomer" data-options="iconCls:'icon-edit',plain:true">修改客户</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" id="deleteCustomer" data-options="iconCls:'icon-remove',plain:true">删除客户</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" id="exportCustomer" data-options="iconCls:'icon-page-excel',plain:true">导出客户</a>
	</div>
	<!-- 数据表格结束  -->
	
	<!-- 添加和修改用户的弹出层 开始 -->
	<div id="dlg" class="easyui-dialog" style="width:500px" data-options="closed:true,modal:true,border:'thin',buttons:'#dlg-buttons'">
        <form id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
            <div style="margin-bottom:10px">
                <input name="identity" id="identity" class="easyui-textbox" required="true" label="身份证号:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="custname" class="easyui-textbox" required="true" label="客户姓名:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="address" class="easyui-textbox" required="true" label="客户地址:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="phone" class="easyui-textbox" required="true"  label="客户电话:" style="width:100%">
            </div>
             <div style="margin-bottom:10px">
                <input name="career" class="easyui-textbox" required="true"  label="客户职位:" style="width:100%">
            </div>
             <div style="margin-bottom:10px">
             	<label class="textbox-label" style="text-align: left;">
        				客户性别:
        			</label>
           			 <input class="easyui-radiobutton" name="sex" value="1">&nbsp;&nbsp;男&nbsp;&nbsp;
           			 <input class="easyui-radiobutton" name="sex" value="0">&nbsp;&nbsp;女&nbsp;&nbsp;
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveCustomer()" style="width:90px">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
    </div>
	
	<!-- 添加和修改用户的弹出层 结束-->

	<script type="text/javascript">
	//加载表格数据
	$("#table").datagrid({
		title:'客户列表',
		singleSelect:true,
		collapsible:true,
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        toolbar:'#tb',
        url:'${ctx}/customer/loadAllCustomer.action',
        columns:[[
                  {field:'identity',title:'身份证号',align:'center',width:100},
                  {field:'custname',title:'客户姓名',align:'center',width:100},
                  {field:'address',title:'客户地址',align:'center',width:100},
                  {field:'sex',title:'客户性别',align:'center',width:100,formatter:function(value){
                	  if(value==1){
                 		 return "<font color=red>男</font>"; 
                 	  }else{
                 		  return "<font color=green>女</font>"; 
                 	  }
                  }},
                  {field:'phone',title:'客户电话',align:'center',width:100},
                  {field:'career',title:'客户职位',align:'center',width:100}
              ]]
       
	});
		//查询
		function doSearch(){
			var params=$("#searchFrm").serialize();
			$("#table").datagrid({
				url:'${ctx}/customer/loadAllCustomer.action?'+params
			});
		};
		//打开添加用户的弹出层
		var url="";
		$("#addCustomer").click(function(){
			$("#dlg").dialog("open").dialog('setTitle','添加客户');
			$("#fm").form("clear");//清空表格
			//设置id可读
			$("#identity").textbox('readonly',false);
			url="${ctx}/customer/addCustomer.action";
		});
		//打开修改的弹出层
		$("#updateCustomer").click(function(){
			//得到当前行
			var row=$("#table").datagrid('getSelected');
			if(row){
				$("#dlg").dialog("open").dialog('setTitle','修改客户');
				//表单数据装载
				$("#fm").form("load",row);
				//设置identity只读
				$("#identity").textbox('readonly',true);
				url="${ctx}/customer/updateCustomer.action";
			}else{
				$.messager.show({
					title:'提示',
					msg:'请选中操作行'
				});
			}
		});
		
		
		//保存
		function saveCustomer(){
			$.messager.progress();	// 显示进度条
			$('#fm').form('submit', {
				url: url,                                  //数据提交地址  user/addCustomer.action
				onSubmit: function(){
					var isValid = $(this).form('validate');//对表单里面的required:true和vailType的数据进行验证
					if (!isValid){
						$.messager.progress('close');	    // 如果验证失败关闭进度条
					}
					return isValid;	                  //返回验证结果
				},
				success: function(obj){               //如果提交成功之后回调的方法
					$.messager.progress('close');	  // 关闭进度条
                    var result = eval('('+obj+')');    //把obj的josn字符串转成js认识的json对象
					$.messager.show({
						title:'提示',
						msg:result.msg
					});
					$("#table").datagrid("reload");    //刷新数据表格
					$("#dlg").dialog("close");         //关闭弹出层
				}
			});
		}
		//删除用户
		$("#deleteCustomer").click(function(){
			//得到当前行
			var row=$("#table").datagrid('getSelected');
			if(row){
				$.messager.confirm('提示','你确定要删除【'+row.custname+'】这个客户吗?',function(r){
                    if (r){
                        $.post('${ctx}/customer/deleteCustomer.action',{identity:row.identity},function(result){
                        	//var resu = eval('('+result+')');    为啥没用
                                $.messager.show({    
                                    title: '提示',
                                    msg: '删除成功'
                                });
                                $("#table").datagrid("reload");//刷新表格
                        },'json');
                    }
                });
			}else{
				$.messager.show({
					title:'提示',
					msg:'请选中操作行'
				});
			}
		});
	
		//导出客户数据
		$("#exportCustomer").click(function(){
			//得到查询条件
			var params=$("#searchFrm").serialize();
			window.location.href="${ctx}/customer/exportCustoner.action?"+params;
		});
		
	</script>
</body>
</html>
